use alsa::pcm::*;
use alsa::{Direction, ValueOr, Error};

use pyo3::prelude::*;

#[pyclass]
pub struct Recorder {
    pub pcm: PCM,
    pub channels: u32,
    pub rate: u32,
}

#[pymethods]
impl Recorder {
    #[new]
    pub fn default() -> Self {
       let rec = Self {
            pcm: PCM::new("default", Direction::Capture, false).expect("Can not init micro"),
            channels: 1,
            rate: 16000,
       };
       rec.configure();
       return rec;
    }

    pub fn configure(&self) {
        let hwp = HwParams::any(&self.pcm).unwrap();
        hwp.set_channels(self.channels).unwrap();
        hwp.set_rate(self.rate, ValueOr::Nearest).unwrap();
        hwp.set_format(Format::s16()).unwrap();
        hwp.set_access(Access::RWInterleaved).unwrap();
        self.pcm.hw_params(&hwp).expect("Failed to configre micro");
    }

    pub fn info(&self) {
        println!("Name is {}", self.pcm.info().unwrap().get_name().unwrap());
    }

    pub fn record(&self) {}
}
